<?php
/**
 * @author yuanyitian
 * @date 2024/9/2 0:01
 */

namespace api\wechat;

use utils\base64_to_file;
use utils\oss;
use utils\redis;

class category
{
    public function getAll($request)
    {

        //登录检测，未注册的自动注册为默认用户
        if (empty($request['openid'])) {
            return out_error('非法访问！');
        }
//
//        $this->checkLogin($request);

        $con = con();
        if (isset($request['pre_time'])) {
            $pre_time = strtotime($request['pre_time']);
            $date = date('Y-m-d', $pre_time);
            $time = $pre_time - strtotime($date);
        } else {
            $date = date('Y-m-d');
            $time = time() - strtotime($date);
        }
        $category = mysqli_query($con, "select * from setting where id in (1,2,3) and value_1<=$time and value_2>$time");
        $category = mysqli_fetch_all($category, true);
        if (count($category) == 0) {
            mysqli_close($con);
            return out_error('未在用餐时间！');
        }
        $type = (int)$category[0]['id'];
        $start_time = strtotime($date) + $category[0]['value_1'];
        $end_time = strtotime($date) + $category[0]['value_2'];
        $menu_id = mysqli_query($con, "select * from menu where is_delete=1 and status=1 and date like '%$date%'");
        $menu_id = mysqli_fetch_all($menu_id, true);
        if (count($menu_id) == 0) {
            mysqli_close($con);
            return out_error('请后台设置菜单！');
        }
        switch ($type) {
            case 1:
                $vip_price = $menu_id[0]['price_vip_1'];
                $price = $menu_id[0]['price_1'];
                break;
            case 2:
                $vip_price = $menu_id[0]['price_vip_2'];
                $price = $menu_id[0]['price_2'];
                break;
            case 3:
                $vip_price = $menu_id[0]['price_vip_3'];
                $price = $menu_id[0]['price_3'];
                break;
            default:
                mysqli_close($con);
                return out_error('非法访问！');
                break;
        }
        $menu_id = array_column($menu_id, 'id');
        $menu_id = implode(',', $menu_id);
        $goods = mysqli_query($con, "select * from menu_goods_config where is_delete=1 and status=1 and type=$type and menu_id in ($menu_id)");
        $goods = mysqli_fetch_all($goods, true);

        $goods_ids = implode(',', array_column($goods, 'goods_id'));
        if (empty($goods_ids)) {
            mysqli_close($con);
            return out_error('该菜单无商品！');
        }
        $category_ids = mysqli_query($con, "select * from goods_category_config where is_delete=1 and status=1 and goods_id in ($goods_ids)");
        $category_ids = mysqli_fetch_all($category_ids, true);
        if (count($category_ids) == 0) {
            mysqli_close($con);
            return out_error('该时段此菜单无商品！');
        }
        $category_ids = implode(',', array_column($category_ids, 'category_id'));

        $data = mysqli_query($con, "select * from category where is_delete=1 and status=1 and id in ($category_ids) order by `sort` ASC ");
        $data = mysqli_fetch_all($data, true);

        $openid = $request['openid'];
        $user = mysqli_query($con, "select * from user where openid='$openid'");
        $user = mysqli_fetch_all($user, true);
        if (count($user) === 0) {//检测到未注册用户，则自动注册默认用户
            //强制登录
            mysqli_close($con);
            return out_not_login();
//            mysqli_query($con, "insert into user(openid,nickname,avatar,create_time) values('$openid','普通用户','https://luopingcn.oss-cn-chengdu.aliyuncs.com/goods/1/avatar.png',$time)");
//            $user_id=(int)mysqli_insert_id($con);
//            $is_vip=false;
        } else {
            $user_id = (int)$user[0]['id'];
            $res = mysqli_query($con, "select * from user_power_config where status=1 and is_delete=1 and user_id=$user_id and type in (5,7) and check_status=1");
            $res = mysqli_num_rows($res);
            if ($res === 0) {
                $is_vip = false;
            } else {
                $is_vip = true;
            }
        }


        $order = mysqli_query($con, "select * from `order` where user_id=$user_id and time_type=$type and status in (0,1) and is_delete=1 and (create_time between $start_time and $end_time ) ");
        $order = mysqli_num_rows($order);
        mysqli_close($con);

        array_unshift($data, [

            "id" => "0",
            "name" => "全部",
            "type" => "0",
            "sort" => "1",
            "create_time" => "1723558746",
            "update_time" => "1729487808",
            "delete_time" => null,
            "remark" => "",
            "is_delete" => "1",
            "status" => "1"

        ]);

        return out_right([
            'category' => $data,
            'type' => $type,
            'price_vip' => sprintf('%.2f', $vip_price / 100),
            'price' => sprintf('%.2f', $price / 100),
            'is_vip' => $is_vip,
            'order' => $order,
            'user_id' => $user_id
        ]);
    }

    //用户检测
    public function checkLoginByQrcode($request)
    {
       // $selected=(int)$request['selected'];
        $res = (new user())->login($request);
        $res = json_decode($res, true);
        if ($res['code'] !== 200) {
            return json_encode($res);
        }
        $res = $res['data'];
        $token = $res['token'];
        $userinfo = $res['userinfo'];
        $openid = $res['openid'];
        if(isset($request['selected'])){//堂食二维码进入
            $res=[
                'code'=>200,
                'data'=>[
                    'category' => [],
                    'type' => 0,
                    'price_vip' => '0.00',
                    'price' => '0.00',
                    'is_vip' => 1,
                    'order' => 1,
                    'user_id' => (int)$userinfo['id']
                ]
            ];
        }else{//其他
            $res = $this->getAll(['openid' => $openid]);
            $res = json_decode($res, true);
        }
        if ($res['code'] === 200) {
            $res = $res['data'];
            unset($res['category']);

            $user_id = (int)$res['user_id'];

            //获取新创建用户信息
            $con = con();
            $info = mysqli_query($con, "select * from user where openid='$openid'");
            $info = mysqli_fetch_all($info, true);
            $info = $info[0];
            if ($info['phone'] == 0) {
                $info['phone'] = '';
            }
            $info['money'] = sprintf('%.2f', $info['money'] / 100);
            $power = mysqli_query($con, "select * from user_power_config where user_id=$user_id and is_delete=1 and status=1 and check_status=1");
            $power = mysqli_fetch_all($power, true);


            //临时开放权限，若需权限控制，则改为false
            $info['type_1'] = false;
            $info['type_2'] = false;
            $info['type_3'] = false;
            $info['type_4'] = false;
            $info['type_5'] = false;
            $info['type_7'] = false;
            if (count($power) != 0) {
                $power = array_unique(array_column($power, 'type'));
                sort($power);
                for ($i = 0; $i < count($power); $i++) {
                    switch ((int)$power[$i]) {
                        case 1:
                            $info['type_1'] = true;
                            break;
                        case 2:
                            $info['type_2'] = true;
                            break;
                        case 3:
                            $info['type_3'] = true;
                            break;
                        case 4:
                            $info['type_4'] = true;
                            break;
                        case 5:
                            $info['type_5'] = true;
                            break;
                        case 7:
                            $info['type_7'] = true;
                            break;
                    }
                }
            }

            $time = time() - strtotime(date('Y-m-d'));

            //是否配送设置
            $send_status = mysqli_query($con, "select * from setting where id=6");
            $send_status = mysqli_fetch_all($send_status, true);
            if (count($send_status) !== 0 && $send_status[0]['value_1'] === '1') {
                $send_status = true;
            } else {
                $send_status = false;
            }
            //配送价设置
            $send_price = mysqli_query($con, "select * from setting where id=7");
            $send_price = mysqli_fetch_all($send_price, true);
            if (count($send_price) !== 0) {
                $send_price = sprintf('%.2f', $send_price[0]['value_1']);
            } else {
                $send_price = '0.00';
            }
            //检测当前所属时段
            $type = mysqli_query($con, "select * from setting where id in (1,2,3) and value_1<$time and value_2>$time");
            $type = mysqli_fetch_all($type, true);
            $type = (int)$type[0]['id'];
            $res['type']=$type;
            //堂食价格设置
            $fix_price = mysqli_query($con, "select * from setting where id in (12,13,14) order by `id` ASC ");
            $fix_price = mysqli_fetch_all($fix_price, true);
            if (count($fix_price) !== 0) {
                switch ($type) {
                    case 1:
                        $fix_price = sprintf('%.2f', $fix_price[0]['value_1']);
                        break;
                    case 2:
                        $fix_price = sprintf('%.2f', $fix_price[1]['value_1']);
                        break;
                    case 3:
                        $fix_price = sprintf('%.2f', $fix_price[2]['value_1']);
                        break;
                    default:
                        $fix_price = '0.00';
                        break;
                }
            } else {
                $fix_price = '0.00';
            }
            mysqli_close($con);

            $res['token'] = $token;
            $res['openid'] = $openid;
            $res['userinfo'] = $info;
            $res['send_status'] = $send_status;
            $res['send_price'] = $send_price;
            $res['fix_price'] = $fix_price;
            return out_right($res);
        } else {
            $res = $res['data'];
            return out_error($res);
        }
    }

}